package com.example.smartcity1.service.patient;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.example.smartcity1.R;
import com.example.smartcity1.adapter.PatientListAdapter;
import com.example.smartcity1.bean.MyUserInfoBean;
import com.example.smartcity1.bean.PatientListBean;
import com.example.smartcity1.network.RetrofitClient;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PatientListActivity extends AppCompatActivity {

    private ListView listView;
    private FloatingActionButton floatingActionButton;
    private String name;
    private int sex;
    private List<PatientListBean.RowsDTO> rows;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patient_list);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setTitle("就诊人卡片");
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        initView();
        floatingActionButton.setOnClickListener(v -> startActivity(new Intent(this,PatientPageActivity.class)));
    }

    @Override
    protected void onStart() {
        super.onStart();
        SharedPreferences user = getSharedPreferences("user", MODE_PRIVATE);
        RetrofitClient.serviceService().getPatientListData(user.getString("token", ""), user.getString("userId", "1")).enqueue(new Callback<PatientListBean>() {
            @Override
            public void onResponse(Call<PatientListBean> call, Response<PatientListBean> response) {
                PatientListBean body = response.body();
                if (body != null && body.getCode() == 200) {
                    rows = body.getRows();
                    if (body.getTotal() == 0) {
                        RetrofitClient.appService().getMyUserInfoData(user.getString("token","")).enqueue(new Callback<MyUserInfoBean>() {
                            @Override
                            public void onResponse(Call<MyUserInfoBean> call, Response<MyUserInfoBean> response) {
                                MyUserInfoBean body1 = response.body();
                                if (body1 != null && body1.getCode() == 200) {
                                    MyUserInfoBean.UserDTO user1 = body1.getUser();
                                    name = user1.getNickName();
                                    sex = Integer.parseInt(user1.getSex());
                                    PatientListBean.RowsDTO row = new PatientListBean.RowsDTO();
                                    row.setName(name);
                                    row.setSex(sex);
                                    rows.add(row);
                                    PatientListAdapter adapter = new PatientListAdapter(PatientListActivity.this, R.layout.item_patient_list, rows);
                                    listView.setAdapter(adapter);
                                    listView.setOnItemClickListener((parent, view, position, id) -> {
                                        Intent intent = new Intent(PatientListActivity.this,PatientPageActivity.class);
                                        startActivity(intent);
                                    });
                                }
                            }
                            @Override
                            public void onFailure(Call<MyUserInfoBean> call, Throwable t) {
                            }
                        });
                    } else {
                        PatientListAdapter adapter = new PatientListAdapter(PatientListActivity.this, R.layout.item_patient_list, rows);
                        listView.setAdapter(adapter);
                        listView.setOnItemClickListener((parent, view, position, id) -> {
                            Toast.makeText(PatientListActivity.this, "111", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(PatientListActivity.this,PatientPageActivity.class);
                            intent.putExtra("id",rows.get(position).getId());
                            startActivity(intent);
                        });
                    }


                }
            }
            @Override
            public void onFailure(Call<PatientListBean> call, Throwable t) {
            }
        });

    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return super.onSupportNavigateUp();
    }

    private void initView() {
        listView = findViewById(R.id.listView);
        floatingActionButton = findViewById(R.id.floatingActionButton);
    }
}